package com.example.demo.conf;

import javax.servlet.Filter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;

/**
 * @author yujie
 * @create 2018-04-26 下午3:45
 **/
@Configuration
@EnableConfigurationProperties(value = {TestProperties.class})
public class TestConf {

    @Autowired
    private TestProperties testProperties;


    @Bean
    public Filter filter(){
        return new MyFilter();
    }

    @Bean
    @ConditionalOnProperty(prefix = "my.test", name = "ttl")
    public RedisCacheManager redisCacheManager(RedisConnectionFactory redisConnectionFactory) {
        /**
         *
         * 如果配置了这个属性才配置这个bean
         * ConditionalOnProperty 必须配置到具体属性 （prefix + name）组合
         *
         */
        System.out.println("redisCacheManager");
        /**
         * 2.0.1
         * redis 更改了RedisCacheManager的创建方法
         */
        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
        /**
         * 这里为啥重新new了一个对象
         */
        redisCacheConfiguration = redisCacheConfiguration.entryTtl(testProperties.getTtl());
        RedisCacheManager redisCacheManager = new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);
        return redisCacheManager;
    }
}
